package co.gov.medellin.ssti.sub.paoyer.common.exception;

import java.io.StringWriter;
import java.io.PrintWriter;

/**
 * Representa una excepci�n que se genera exclusivamente por un comportamiento
 * no deseado de la aplicaci�n.
 * 
 * Esta clase se crea cuando, por ejemplo, la comunicaci�n con alg�n
 * sub-sistema se ve interrumpida inesperadamente.
 * 
 * @version 0.9.1
 */
public class SystemException extends BaseException {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2344750301342999735L;

	/**
	 * Excepcion creada inicialmente y que causa la excepci�n en primer lugar.
	 */
	private Exception javaException;

	/**
	 * Excepcion creada inicialmente y que causa la excepci�n en primer lugar.
	 */
	private String javaExceptionStackTrace = "";

	/**
	 * Constructores.
	 */

	public SystemException(ModuleVO moduleVO, int messageId) {
		this(moduleVO, messageId, null, null, true);
	}

	public SystemException(ModuleVO moduleVO, int messageId,
			Exception javaException) {
		this(moduleVO, messageId, javaException, null, true);
	}

	public SystemException(ModuleVO moduleVO, int messageId,
			Exception javaException, String customMessage) {
		this(moduleVO, messageId, javaException, customMessage, true);
	}

	public SystemException(ModuleVO moduleVO, int messageId,
			Exception javaException, String customMessage,
			boolean isSendMailAllowed) {

		super(ModuleExceptionFactory
				.getModuleException(moduleVO.getExceptionClass())
				.getExceptionVO(messageId).toString()
				+ "Mensaje Personalizado = " + customMessage);
		this.moduleVO = moduleVO;
		this.messageId = messageId;
		this.customMessage = customMessage;
		this.javaException = javaException;
		/*
		 * if (isSendMailAllowed) { Thread emailThread = new Thread(new
		 * ExceptionSender(this)); emailThread.start(); }
		 */
	}

	/**
	 * Devuelve la Excepcion original, si esta fue guardada.
	 * 
	 * @return Exception
	 */
	public Exception getJavaException() {
		return this.javaException;
	}

	/**
	 * 
	 * @return String
	 */
	public String getJavaStackTrace() {
		String stackTraceContent = "";

		if (this.javaExceptionStackTrace == "") {
			if (this.javaException != null) {
				StringWriter writer = new StringWriter();
				PrintWriter printWriter = new PrintWriter(writer);
				System.err.println("/******** TDSUB - cwsub");
				this.javaException.printStackTrace(printWriter);
				System.err.println("TDSUB - cwsub ********/");
				printWriter.flush();
				stackTraceContent = writer.toString();

				this.javaExceptionStackTrace = stackTraceContent;
			}
		}

		return this.javaExceptionStackTrace;
	}

	/**
	 * Devuelve s�lo un mensaje sobre la excepci�n en cuesti�n.
	 * 
	 * @return String
	 */
	public String getMessage() {
		String javaExceptionInitialToken = "JE=[[";
		String initialToken = "_[[";
		String finalToken = "]] ";
		String javaExceptionString = "";
		String stackTraceContent = "";

		if (this.javaException != null) {
			stackTraceContent = this.getJavaStackTrace();

			javaExceptionString = javaExceptionInitialToken
					+ this.javaException.getClass().getName() + finalToken
					+ initialToken + stackTraceContent + finalToken;
		}

		return super.getMessage() + javaExceptionString;
	}

}